package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.Address;
import com.alcj.model.User;
import com.alcj.service.AddressService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by jdz on 2017/11/7.
 */

@Controller
@RequestMapping("/orderDetailList")
public class OrderDetailList {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AddressService addressService;


    /**
     * @param request
     * @param model
     * @return String
     * @Author: guofsh
     * @Description:初期表示信息
     */
    @RequestMapping("/queryOrderDetai")
    public String queryAddress(HttpServletRequest request, Model model) {
        try {
            // 获取登录用户
            User sysUser =  new User();
            sysUser.setUserid("11");
//            User sysUser = (User) request.getSession().getAttribute("User");
            // 获取登录用户判断用户Id跟用户类型是否为空
            if (CmnUtil.isEmpty(sysUser)) {

                return SysConstant.INDEX_PAGE;

            } else {
                //取得用户Id
                String sysUserId = sysUser.getUserid();
                request.getSession().setAttribute("userId", sysUserId);
                // 查询用户地址信息
                List<Address> list = addressService.getAddressInfos(sysUserId);
                model.addAttribute("listAddressSize", list.size());
                model.addAttribute("Address", new Address());
                //把用户地址信息存入model
                model.addAttribute("listAddress", list);
            }

        } catch (Exception e) {
            // 异常报错
            logger.error("获取收货地址信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :", e);
            return SysConstant.ERROR_PAGE_500;
        }
        return "/orderDetailList";
    }
}

